package ACWing.Dynamicprogramming;
//	82	圆圈中最后剩下的数字
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author :chenjie
 * @date :Created 2022/12/3 16:02
 */
public class ThelastNumberleftintheCircle {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        System.out.println(remove(n, m));
    }
    public static int remove(int n,int m){

        ArrayList<Integer> arr=new ArrayList<>();
        for (int i = 0; i < n; i++) {
            arr.add(i);
        }
        int x=0;
        while (arr.size()>1){
            x=(x+m-1)%arr.size();
//            System.out.println("((m-1)*i)"+((m-1)*i)+"arr.size()"+arr.size()+"  "+x);

            arr.remove(x);
//            System.out.println(arr);
            if(arr.size()==1){
                break;
            }
        }
        return arr.get(0);
    }
}
